package com.zzj.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzj.bean.CustomerBean;
import com.zzj.bean.OrdersBean;
import com.zzj.dao.CustomerDao;
import com.zzj.dao.OrdersDao;
import com.zzj.utils.Result;
import com.zzj.utils.StatusCode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class OrdersServiceImpl implements OrdersService {

    @Resource
    private OrdersDao ordersDao;
    @Resource
    private CustomerDao customerDao;

    @Override
    public Result addOrders(Map map) {
        CustomerBean customerBean = new CustomerBean();
        customerBean.setCname((String) map.get("cname"));
        customerBean.setCuser((String) map.get("cuser"));
        customerBean.setCtel((String) map.get("ctel"));
        customerBean.setCperson((String) map.get("cperson"));
        customerBean.setCaddr((String) map.get("caddr"));
        int i2 = customerDao.addCustomer(customerBean);

        OrdersBean ordersBean = new OrdersBean();
        ordersBean.setOstate("0");
        ordersBean.setOamount(0);
        ordersBean.setOoid((String) map.get("ooid"));
        ordersBean.setDtime(dateFormat((String) map.get("dtime")));
        ordersBean.setJtime(dateFormat((String) map.get("jtime")));
        ordersBean.setOrequire((String) map.get("orequire"));
        ordersBean.setCid(customerBean.getCid());

        System.out.println("ordersBean = " + ordersBean);
        System.out.println("customerBean = " + customerBean);

        System.out.println("cid = " + customerBean.getCid());
        int i1 = ordersDao.addOrders(ordersBean);
        if (i1 == 1 && i2 == 1){
            return new Result();
        }
        return new Result(false, StatusCode.ERROR,"");
    }

    @Override
    public PageInfo<OrdersBean> findAll(int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<OrdersBean> all = ordersDao.findAll();
        return new PageInfo<>(all);
    }

    @Override
    public Result queryCountByOoid(int ooid) {
        int i = ordersDao.queryCountByOoid(ooid);
        if (i > 0){
            return new Result();
        }
        return new Result(false,StatusCode.ERROR,"没有查找到数据");
    }

    /**
     * 日期格式化
     * @param datetime
     * @return
     */
    private String dateFormat(String datetime){
        return datetime.substring(0,datetime.indexOf('T'));
    }
}
